import java.util.ArrayList;
import java.util.List;

public class Permute {

    public static void main(String[] args) {
        Permute a = new Permute();
        int[] nums = {1, 2, 3};
        System.out.println(a.permute(nums));
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        ArrayList<Integer> temp = new ArrayList<>();
        boolean[] flag = new boolean[20];
        process(nums, 0, temp, ans, flag);
        return ans;
    }

    public void process(int[] nums, int i, List<Integer> temp, List<List<Integer>> list, boolean[] flag) {
        if (i == nums.length) {
            list.add(new ArrayList<>(temp));
            return;
        }
        for (int j = 0; j < nums.length; j++) {
            if (!flag[nums[j] + 10]) {
                flag[nums[j] + 10] = true;
                temp.add(nums[j]);
                process(nums, i + 1, temp, list, flag);
                flag[nums[j] + 10] = false;
                temp.remove(temp.size() - 1);
            }
        }
    }
}
